home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 4664 / 4664.xpi / chrome / twitterbar.jar / content / shorteners.js < prev   
Text File  |  2010-02-08  |  4KB  |  181 lines

  1. var TWITTERBAR_SHORTENERS = {
  2.     prefs : Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("extensions.twitter."),
  3.     
  4.     shortenUrls : function (status, callback) {
  5.         var shortener = TWITTERBAR_SHORTENERS.prefs.getCharPref("shortener");
  6.         
  7.         if (shortener == "") {
  8.             callback(status);
  9.         }
  10.         else if (shortener == "tinyurl") {
  11.             this.shortenUrlsTiny(status, callback);
  12.         }
  13.         else if (shortener == "bitly") {
  14.             this.shortenUrlsBitly(status, callback);
  15.         }
  16.         else {
  17.             this.shortenUrlsIsGd(status, callback);
  18.         }
  19.     },
  20.     
  21.     getUrlLength : function (url) {
  22.         var shortener = TWITTERBAR_SHORTENERS.prefs.getCharPref("shortener");
  23.         
  24.         if (shortener == "tinyurl") {
  25.             return 25;
  26.         }
  27.         else if (shortener == "bitly") {
  28.             return 20;
  29.         }
  30.         else if (shortener == "") {
  31.             return url.length;
  32.         }
  33.         else {
  34.             // is.gd
  35.             return 18;
  36.         }
  37.     },
  38.     
  39.     shortenUrlsTiny : function (status, callback) {
  40.         status = status + " ";
  41.         
  42.         var urlsToShorten = [];
  43.         
  44.         function shortenNextUrl() {
  45.             if (urlsToShorten.length == 0) {
  46.                 callback(status.replace(/^\s+|\s+$/g, ""));
  47.             }
  48.             else {
  49.                 var nextUrl = urlsToShorten.shift();
  50.  
  51.                 var req = new XMLHttpRequest();
  52.                 req.open("GET", "http://tinyurl.com/api-create.php?url=" + nextUrl, true);
  53.  
  54.                 req.onreadystatechange = function () {
  55.                     if (req.readyState == 4) {
  56.                         if (req.status == 200) {
  57.                             try {
  58.                                 var shortUrl = req.responseText;
  59.                             
  60.                                 status = status.replace(nextUrl + " ", shortUrl + " ");
  61.                             } catch (e) {
  62.                             }
  63.                         }
  64.  
  65.                         shortenNextUrl();
  66.                     }
  67.                 };
  68.  
  69.                 req.send(null);
  70.             }
  71.         }
  72.         
  73.         var urlRE = /(https?:\/\/[\S]+)\s/ig;
  74.         var url;
  75.         
  76.         while ((url = urlRE.exec(status)) != null) {
  77.             urlsToShorten.push(url[1]);
  78.         }
  79.         
  80.         shortenNextUrl();        
  81.     },
  82.     
  83.     shortenUrlsBitly : function (status, callback) {
  84.         status = status + " ";
  85.         
  86.         var urlsToShorten = [];
  87.         
  88.         function shortenNextUrl() {
  89.             if (urlsToShorten.length == 0) {
  90.                 callback(status.replace(/^\s+|\s+$/g, ""));
  91.             }
  92.             else {
  93.                 var nextUrl = urlsToShorten.shift();
  94.  
  95.                 var req = new XMLHttpRequest();
  96.                 
  97.                 var apiKey = TWITTERBAR_SHORTENERS.prefs.getCharPref("bitlyApiKey");
  98.                 var login = TWITTERBAR_SHORTENERS.prefs.getCharPref("bitlyLogin");
  99.                 
  100.                 if (!apiKey || !login) {
  101.                     apiKey = "R_14789c4220ade2b5d8616d3bc5b955a7";
  102.                     login = "twitterbar";
  103.                 }
  104.                 
  105.                 var url = "http://api.bit.ly/shorten?version=2.0.1&longUrl=" + encodeURIComponent(nextUrl);
  106.                 url += "&login=" + encodeURIComponent(login) + "&apiKey=" + encodeURIComponent(apiKey);
  107.                 
  108.                 req.open("GET", url, true);
  109.  
  110.                 req.onreadystatechange = function () {
  111.                     if (req.readyState == 4) {
  112.                         if (req.status == 200) {
  113.                             try {
  114.                                 var json = JSON.parse(req.responseText);
  115.                                 
  116.                                 if (json.errorCode == 0) {
  117.                                     var shortUrl = json.results[nextUrl].shortUrl;
  118.                             
  119.                                     status = status.replace(nextUrl + " ", shortUrl + " ");
  120.                                 }
  121.                             } catch (e) {
  122.                             }
  123.                         }
  124.  
  125.                         shortenNextUrl();
  126.                     }
  127.                 };
  128.  
  129.                 req.send(null);
  130.             }
  131.         }
  132.         
  133.         var urlRE = /(https?:\/\/[\S]+)\s/ig;
  134.         var url;
  135.         
  136.         while ((url = urlRE.exec(status)) != null) {
  137.             urlsToShorten.push(url[1]);
  138.         }
  139.         
  140.         shortenNextUrl();        
  141.     },
  142.     
  143.     shortenUrlsIsGd : function (status, callback) {
  144.         status = status + " ";
  145.         
  146.         var urlsToShorten = [];
  147.         
  148.         function shortenNextUrl() {
  149.             if (urlsToShorten.length == 0) {
  150.                 callback(status.replace(/^\s+|\s+$/g, ""));
  151.             }
  152.             else {
  153.                 var nextUrl = urlsToShorten.shift();
  154.  
  155.                 var req = new XMLHttpRequest();
  156.                 req.open("GET", "http://is.gd/api.php?longurl=" + nextUrl, true);
  157.  
  158.                 req.onreadystatechange = function () {
  159.                     if (req.readyState == 4) {
  160.                         if (req.status == 200) {
  161.                             status = status.replace(nextUrl + " ", req.responseText + " ");
  162.                         }
  163.  
  164.                         shortenNextUrl();
  165.                     }
  166.                 };
  167.  
  168.                 req.send(null);
  169.             }
  170.         }
  171.         
  172.         var urlRE = /(https?:\/\/[\S]+)\s/ig;
  173.         var url;
  174.         
  175.         while ((url = urlRE.exec(status)) != null) {
  176.             urlsToShorten.push(url[1]);
  177.         }
  178.         
  179.         shortenNextUrl();
  180.     }
  181. };